package com.herui.iot.ui.dialog;

import android.app.Dialog;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.herui.device_lib.listener.DeviceLis;
import com.herui.device_lib.listener.DeviceLisAdapter;
import com.herui.device_lib.presenter.DevicePresenterInter;
import com.herui.sdyu_lib.adapter.recycle_view.base.ViewHolder;
import com.herui.sdyu_lib.base.BaseActivity;
import com.herui.sdyu_lib.utils.ToastUtil;
import com.herui.sport_care.R;
import com.herui.sport_care.ui.BaseListView;
import com.herui.sport_care.utils.ViewTools;
import com.protontek.healthy.bluetoothcore.ble.scanner.bean.ScanResult;

import java.util.List;

import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by amanda.shan on 2022/12/6.
 */
public class SpoManagerDialog {

    @BindView(R.id.view_scan)
    View view_scan;
    @BindView(R.id.id_recyclerview)
    RecyclerView id_recyclerview;

    private Dialog dialog;
    private DialogLis dialogLis;

    private DevicePresenterInter devicePresenter;
    BaseListView baseListView;

    /**
     * @param context
     */
    public SpoManagerDialog(BaseActivity context, DevicePresenterInter devicePresenter,
                            DialogLis dialogLis) {

        this.dialogLis = dialogLis;
        this.devicePresenter = devicePresenter;

        View layout = (View) context
                .getLayoutInflater().inflate(R.layout.dialog_spo,
                        null);
        ButterKnife.bind(this, layout);
        dialog = new Dialog(context, R.style.MyDialogStyleBottom);
        dialog.setContentView(layout);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setOnDismissListener(dialog1 -> {
            DeviceLisAdapter.remove(view);
        });
        dialog.show();
        Window dialogWindow = dialog.getWindow();
        dialogWindow.setLayout((int) ViewTools.fromDpToPx(350), WindowManager
                .LayoutParams.WRAP_CONTENT);
        dialogWindow.setGravity(Gravity.CENTER);
        initUi(context);

        DeviceLisAdapter.add(view);
        devicePresenter.scanSpo();
    }

    private void initUi(BaseActivity context) {
        view_scan.setVisibility(View.VISIBLE);
        baseListView = new BaseListView<ScanResult>(context) {
            @Override
            protected void convert(ViewHolder holder, ScanResult value, int position) {
                if (value != null) {
                    holder.setText(R.id.tv_mac_name, value.getDevice().getName());
                    holder.setText(R.id.tv_mac, value.getDevice().getAddress());
                    holder.setOnClickListener(R.id.btn_connect, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            devicePresenter.connectSpo(value.getDevice().getAddress());
                        }
                    });
                } else {
                    holder.setText(R.id.tv_mac_name, "血氧");
                    holder.setText(R.id.tv_mac, "123456789");
                    holder.setOnClickListener(R.id.btn_connect, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            devicePresenter.connectSpo("123456789");
                        }
                    });
                }
            }

            @Override
            public void doRefresh() {

            }

            @Override
            public void addData(ScanResult scanResult) {
                List<ScanResult> scanResults = getmDatas();
                boolean flag = false;
                for (int i = 0; i < scanResults.size(); i++) {
                    if (scanResults.get(i).getDevice().getAddress().equals(scanResult.getDevice().getAddress())) {
                        flag = true;
                        break;
                    }
                }
                if (!flag) {
                    super.addData(scanResult);
                }
            }
        };
        baseListView.initList(id_recyclerview, R.layout.item_spo_list, false, null);
    }

    DeviceLis view = new DeviceLis() {
        @Override
        public void onSpoDevice(ScanResult scanResult) {
            view_scan.post(() -> {
                baseListView.addData(scanResult);
            });
        }

        @Override
        public void onConnectSpo(boolean flag) {
            view_scan.post(() -> {
                if (flag) {
                    dismiss();
                } else {
                    ToastUtil.showMessage("连接失败");
                }
            });
        }

    };

    public void setDialogLis(DialogLis dialogLis) {
        this.dialogLis = dialogLis;
    }

    public void dismiss() {
        dialog.dismiss();
    }

    public void show() {
        dialog.show();
    }

    public interface DialogLis {
        void onOk();
    }
}

